import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner scanner;
//		try {
			//scanner = new Scanner(new File(args[0]));
			//scanner = new Scanner(new File("data/stacking-027.in"));
			scanner = new Scanner(System.in);
			int caseNum = 0;
			while (scanner.hasNext()) {
				caseNum++;
				int n = scanner.nextInt();
				ArrayList<Stack> stacks = new ArrayList<Stack>();
				for (int i = 0; i < n; i++) {
					int size = scanner.nextInt();
					Stack stack = new Stack(size);
					for (int j = 0; j < size; j++) {
						stack.addPlate(scanner.nextInt());
					}
					stacks.add(stack);
				}

				int moveNum = 0;
				// Stack prevMax = null;
				Stack curMax = Collections.max(stacks);
				while (curMax.index != -1) {
					if (curMax.index == 0)
						if (Stack.lastStackUsed != curMax)
							moveNum++;
						else
							moveNum--;
					else if (Stack.lastStackUsed != curMax)
						moveNum += 2;
					
					//System.out.print(stacks.indexOf(curMax)+"\t");
					curMax.movePlate();
					
					Stack.lastStackUsed = curMax;
					curMax = Collections.max(stacks);
				}
				if (moveNum > 0)
					moveNum--;
//				FileWriter fstream = new FileWriter("answer.txt");
//				BufferedWriter out = new BufferedWriter(fstream);
//				out.write("Case " + Integer.toString(caseNum) + ": "
//						+ Integer.toString(moveNum) + "\n");
//				out.close();
				System.out.println("Case " + Integer.toString(caseNum) + ": "
						+ Integer.toString(moveNum));
			}
			scanner.close();
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}

	static class Stack implements Comparable<Stack> {
		private int[] plates;
		int index;

		static Stack lastStackUsed;

		void addPlate(int diameter) {
			this.plates[++index] = diameter;
		}

		void movePlate() {
			//System.out.println(this.plates[index]);
			index--;
		}

		int getPlate(int i) {
			if (i < 0) {
				return 0;
			} else {
				return plates[i];
			}
		}

		public Stack(int size) {
			plates = new int[size];
			index = -1;
		}

		@Override
		public int compareTo(Stack s) {
			int thisIndex = this.index;
			int sIndex = s.index;
			int cmp = getPlate(thisIndex) - s.getPlate(sIndex);
			if (cmp == 0) {
				if (this != lastStackUsed && s != lastStackUsed) {
					int invert = 1;
					while (thisIndex != -1) {
						while(sIndex>=0&&s.getPlate(sIndex)==s.getPlate(sIndex-1))
							sIndex--;
						while(thisIndex>=0&&getPlate(thisIndex)==getPlate(thisIndex-1))
							thisIndex--;
						cmp = s.getPlate(--sIndex) - getPlate(--thisIndex);
						if (cmp != 0)
							return invert*cmp;
						invert*=-1;
					}
					return 0;
				}
				return this == lastStackUsed ? 1000 : -1000;
			}
			return cmp;
		}
	}

}
